package server

import (
	"fmt"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"log"
	"os"
)

var productCmd = &cobra.Command{
	Use:     "product [name]",
	Short:   "product data",
	Long:    `Manage data products`,
	Example: `product report --date=20240701`,
	PreRun: func(cmd *cobra.Command, args []string) {
		log.Print("product start")
	},
	Args: cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		//fmt.Println("current env:", viper.Get("Env"))
		//fmt.Printf("product data name:%s, date:%s \n", args[0], Date)
		generateData(args[0], Date, number)
	},
	PostRun: func(cmd *cobra.Command, args []string) {
		log.Print("product end")
	},
}

func generateData(name, date string, n int) {
	path := viper.GetString("DataPath")
	for i := 0; i < n; i++ {
		filename := path + fmt.Sprintf("%s-%s-%d", name, date, i)
		_, err := os.Create(filename)
		if err != nil {
			log.Fatal(err)
		}
		log.Println("create file:", filename)
	}
}

var number int

func init() {
	productCmd.Flags().IntVarP(&number, "number", "n", 1, "生产的数量")
}
